/**
 * Created by djh on 2016/11/17.
 * 首页js
 *
 */

define(function(require,exports){
   var common = require("common");
   var template = require("template");
   var pro = require("pro");
   /**
    * 幻灯焦点广告
    * @param {$} $obj 幻灯广告对象
    * @param {String} [speed="slow"] 幻灯广告切换速度
    * @param {String} [autoTime=3000] 幻灯广告停留时间
    * @example
    *      common.focusSlide($("#focusSlide"));
    * */
   exports.focusSlide = function($obj,speed,autoTime){
      var speed=speed || "slow";
      var autoTime=autoTime || 5000;
      var currentindex=0;
      var length=$obj.find(".item").length;
      if(length<2){
         $obj.find(".item").show();
         return;
      }
      //按钮导航
      var $btns=$('<div class="btns"></div>');
      var btnHtml='';
      for(var i=0; i<length; i++){
         btnHtml += '<a href="javascript:;">'+(i+1)+'</a>';
      }
      $btns.html(btnHtml);
      $obj.append($btns);
      var $item=$obj.find(".item");
      var $btn=$btns.find("a");
      $item.eq(currentindex).show();
      $btn.eq(currentindex).addClass("on");

      //切换
      var fn = function(index){
         $item.hide();
         $btn.removeClass("on");
         $item.eq(index).stop().fadeIn(speed);
         $btn.eq(index).addClass("on");
      };

      //设置上一个背景（解决切换时有空白出现）
      var setBg = function(index){
         var url=$item.eq(index).find("img").attr("src");
         $obj.find(".wrap").css({"background":"url("+url+") no-repeat center 0"});
      };
      /*下一个*/
      var prevFn=function(){
         setBg(currentindex);
         currentindex--;
         if(currentindex<0){
            currentindex=length-1;
         }
         fn(currentindex);
      };

      /*下一个*/
      var nextFn=function(){
         setBg(currentindex);
         currentindex++;
         if(currentindex==length){
            currentindex=0;
         }
         fn(currentindex);
      };

      //手动点击
      $obj.find(".btns a").each(function(index, element) {
         $(this).on("click",function(){
            currentindex=index;
            fn(index);
         });
      });

      //添加arrows 左右点击
      $obj.find(".arrow .left").on("click",function(){
         prevFn(currentindex);
      });
      $obj.find(".arrow .right").on("click",function(){
         nextFn(currentindex);
      });

      //自动播放
      var timer=setInterval(nextFn, autoTime);
      $obj.on({
         mouseenter:function(){
            clearInterval(timer);
         },
         mouseleave:function(){
            timer=setInterval(nextFn, autoTime);
         }
      });
   };

   exports.menuSlide = function($obj) {
      var opt = {
         width: 140,
         default_width:280,
         extra:30,
         speed: 200,
         rebound_speed: 300
      };

      var $obj = $($obj),_prev,
          $ul = $obj.find("ul"),
            _a =  $ul.find("li"),

          _origin= $ul.find(".active").index(),
          $active = $ul.find(".active"),
      _move = $obj.find(".moveBg"),
          _extra = opt.extra,
          _current =_prev=_origin;



      var move=function(){
         _move.clearQueue();
         console.log(_prev,_current);
          if(_current==_origin) {
              $active.css("color","#fff");
          }else {
              $active.css("color","#338bf1");
          }
         if(_prev>_current) {
            _extra = -opt.extra;
         }else {
            _extra = opt.extra;
         }
         _prev = _current;
      /*   _move.queue(function(){
            $(this).stop(true,true).animate({"left": ""+Number(opt.default_width+(index*opt.width)+_extra)+""},opt.speed);
            $(this).stop(true,true).animate({"left": ""+Number(opt.default_width+(index*opt.width))+""},opt.rebound_speed,function(){
               $(this).dequeue();
            });
         });*/
/*         _move.queue(function(){
            $(this).stop(true,true).animate({"left": ""+Number(opt.default_width+(index*opt.width)+opt.extra)+""},opt.speed),
                function(){_move.dequeue();};

         });
         _move.queue(function(){
            _move.stop(true,true).animate({"left": ""+Number(opt.default_width+(index*opt.width))+""},opt.rebound_speed),
                function(){_move.dequeue();};
         });*/

         //3
/*         _move.queue(function () {
            //console.info(Number(opt.default_width + (_current * opt.width) + opt.extra));
            $(this).stop(true, false).animate({"left": "" + Number(opt.default_width + (_current * opt.width) + _extra) + ""}, opt.speed);
                $(this).dequeue();
         });
         _move.queue(function () {
            //console.error(Number(opt.default_width + (_current * opt.width)));
            $(this).stop(false, false).animate({"left": "" + Number(opt.default_width + (_current * opt.width)) + ""}, opt.rebound_speed);
            $(this).dequeue();
         });*/

         _move.animate({"left": "" + Number(opt.default_width + (_current * opt.width) + _extra) + ""}, opt.speed);
         _move.animate({"left": "" + Number(opt.default_width + (_current * opt.width)) + ""}, opt.rebound_speed);
      };

       _a.each(function (i, e) {
           $(e).on("mouseenter", function () {
               _current = i;
               move();
           });
       });
       $ul.on("mouseleave", function () {
           _current = _origin;
           move();
       });
   };
   exports.indexInit = function(){

      common.topMenu(0);
      //菜单mouseenter 效果
      exports.menuSlide(".bot_menu");
      //加购物车，直接购买初始化
      pro.buy(exports.username);
      //注册slide
      exports.focusSlide($("#focusSlide"));
      exports.focusSlide($("#focusSlide2"));

      //塑料分类点击
      $(".pro_list").each(function(){
         var $ul = $(this).find(".wrap ul");
         var $li = $(this).find(".header li").not(".more");
         exports.carousel($ul,$li);
      });
   };

   /*tab 切换效果*/
   exports.carousel = function($ul,$li){
      var ul = $ul,width=0;
      var that = this;
      this.init = function(ul){
         ul.find("li").each(function(i,e){
            width+=e.offsetWidth;
         });
         ul.css("width",width);
      };
      this.init(ul);
      this.translate = function(li,ul){

         var id = li.data("id");
         var type = li.data("type");
         var pro = li.data("pro");
         if(li.hasClass("loaded")) {
            var target_li = ul.find("[data-id='"+id+"']");
            var left = target_li.position().left;
            ul.animate({left:-left});
            li.addClass("active").siblings().removeClass("active");
            return false;
         }else {

             common.ajaxHandle({
             url:"/index/index/productsToJson",
             data:{type_name:type,pro_name:pro},
             callback:function(item){

                   item.liId = id;
                   var temp = template("tabTemp",item);
                   ul.append(temp);
                   li.addClass("loaded");
                   that.init(ul);
                   that.translate(li,ul);
               }
             });
         }

      };

      $li.on("click",function(){
         var $ul = $(this).parents(".pro_list").find(".wrap ul");
         that.translate($(this),$ul);
      });
   };
});

